package com.study.tiger.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @Author: liutingting
 * @Date: 2022/3/7 000716:26
 * @Version: 1.0
 */
@Component
//@RefreshScope
public class RedisUtils {

    @Value("${redis.key.prefix}")
    private String keyPrefix;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 判断key
     * @param key
     * @return boolean
     */
    public boolean hasKey(String key) {
        key = keyPrefix + "_" + key;
        return redisTemplate.hasKey(key);
    }

    /**
     * 保存
     * @param key
     * @param value
     * @param time 时间
     * @param unit 单位
     */
    public void insert(String key, String value, long time, TimeUnit unit) {
        key = keyPrefix + "_" + key;
        redisTemplate.opsForValue().set(key, value, time, unit);
    }

    /**
     * 永久保存
     * @param key
     * @param value
     */
    public void insertEver(String key, String value) {
        key = keyPrefix + "_" + key;
        redisTemplate.opsForValue().set(key, value);
    }

    /**
     * 查询value
     * @param key
     * @return String
     */
    public String getValue(String key) {
        key = keyPrefix + "_" + key;
        return redisTemplate.opsForValue().get(key);
    }

    /**
     * 删除单个
     * @param key
     */
    public void deleteKey(String key) {
        key = keyPrefix + "_" + key;
        redisTemplate.delete(key);
    }
}